###################################################################
# Author: Mu yanru
# Date  : 2019.2
# Email : muyanru345@163.com
###################################################################

# Import future modules
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from NodeGraphQt import NodeGraph, constants
# Import third-party modules
from PySide2.QtWidgets import QVBoxLayout
from Qt import QtWidgets
from dayu_widgets.field_mixin import MFieldMixin

from test2.common import Common
from test2.tab.graph_tool_bar import GraphToolBar


class GraphTab(QtWidgets.QWidget, MFieldMixin):
    def __init__(self, parent=None):
        super(GraphTab, self).__init__(parent)
        self._init_ui()

    def _init_ui(self):
        # create graph controller.
        self.graph = NodeGraph()
        # graph.set_background_color(202, 230, 202)
        self.graph.set_grid_mode(constants.ViewerEnum.GRID_DISPLAY_NONE.value)
        self.graph.set_pipe_style(constants.PipeLayoutEnum.STRAIGHT.value)
        # set up context menu for the node graph.
        self.graph.set_context_menu_from_file('./doc/hotkeys.json')
        Common.reload_script(self.graph)

        # wire function to "node_double_clicked" signal.
        self.graph.node_double_clicked.connect(self.show_node_dialog)

        # show the node graph widget.
        graph_widget = self.graph.widget
        graph_widget.resize(1100, 800)
        # auto layout nodes.
        self.graph.auto_layout_nodes()

        graph_tool_bar = GraphToolBar()

        main_lay = QVBoxLayout()
        main_lay.addWidget(graph_tool_bar)
        main_lay.addWidget(graph_widget)
        self.setLayout(main_lay)

    def show_node_dialog(self, node):
        node.show(True)
